<?php 
include('includes/confirm.php');
require_once 'classes/Mysql.php';
$mysql = New Mysql();
$page_name = media;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
    <?php include('includes/head.php'); ?>
<body>
<div id="wrapper">
    <?php include('includes/header.php'); ?>
    <?php include('includes/menu.php'); ?>
    
  <div id="page">
    <div id="page-bgtop">
      <div id="content">
        <div id="browse_result">
			<table class="sortable" id=anyid cellpadding="0" cellspacing="0">
				<tr>
					<th>Title</th>
                    <th>Year</th>
                    <th>Resource Type</th>
					<th class="unsortable">Avail. to Borrow</th>
				</tr>
				<?php
                    $resource_type = isset($_POST['type'])? $_POST['type'] : array();
                    $resource_type_CSV = implode(',',$resource_type);
                    
                    $resource_format = isset($_POST['format'])? $_POST['format'] : array();
                    $resource_format_CSV = implode(',',$resource_format);
                    
                    /* //Debug print outs
                    print "<br/>";
                    print_r($resource_type);
                    print "<br/>";
                    print "<br/>".$resource_type_CSV."<br/>";
                    
                    print "<br/>";
                    print_r($resource_format);
                    print "<br/>";
                    print "<br/>".$resource_format_CSV."<br/>";
                    */
                    
					$query = "SELECT r.id, r.title, r.year, t.name FROM resource as r, resource_type as t
                                WHERE r.type_id=t.id ";
					
					
					//IN OWNER
					if(isset($_POST['mine']) || isset($_POST['friend'])){
						$query .= " AND r.id IN ( SELECT rrr.id FROM resource rrr WHERE ";
						//MINE					
						if(isset($_POST['mine'])){
							$query .= " rrr.id IN( SELECT rr.id FROM resource as rr, resource_copy as cc WHERE rr.id=cc.resource_id ";						
							//User can CAN VIEW copy
							$query .= " AND cc.u_id = '".$_SESSION['userid']."' ) ";
						}
						if(isset($_POST['mine']) && isset($_POST['friend'])) $query .= " OR ";
						//FRIEND
						if(isset($_POST['friend'])){
							$query .= " rrr.id IN ( SELECT rr.id FROM resource as rr, resource_copy as cc WHERE rr.id=cc.resource_id  ";						
							//User can CAN VIEW copy
							$query .= " AND cc.u_id IN (SELECT id1 FROM friend WHERE id2='".$_SESSION['userid']."' AND cc.share_level <= view_setting ) ) ";
						}
						$query .= " ) ";
					}
					
					//IN VIEW SETTING 
					if(isset($_POST['canborrow']) || isset($_POST['loaned'])){
					$query .= " AND r.id IN ( SELECT rrr.id FROM resource rrr WHERE ";
					//CAN BORROW
					if(isset($_POST['canborrow'])){
						$query .= " rrr.id IN ( SELECT rr.id FROM resource as rr, resource_copy as cc WHERE rr.id=cc.resource_id ";						
						//User can CAN BORROW copy
						$query .= " AND cc.u_id IN (SELECT id1 FROM friend WHERE id2='".$_SESSION['userid']."' AND cc.share_level <= borrow_setting ) ) ";
					}
					if(isset($_POST['canborrow']) && isset($_POST['loaned'])) $query .= " OR ";
					//LOANED OUT               ------------- TODO
					if(isset($_POST['loaned'])){
						$query .= " rrr.id IN ( SELECT rr.id FROM resource as rr, resource_copy as cc WHERE rr.id=cc.resource_id ";						
						//User can CAN BORROW copy
						$query .= " AND cc.u_id IN (SELECT id1 FROM friend WHERE id2='".$_SESSION['userid']."' AND cc.share_level <= borrow_setting ) ) ";
					}
					
					$query .= " ) ";
					}
					
					// TYPE AND FORMAT AND SEARCH STRING (RESTRICT EVERYTHING)
					//TYPE
                    if(isset($_POST['type'])){
						$query .= " AND r.type_id IN ( Select id  FROM resource_type WHERE id IN (";
						$query .= $resource_type_CSV."))";
                    }
					
					//FORMAT
					if(isset($_POST['format'])){
						$query .= " AND r.id IN ( SELECT rr.id FROM resource as rr, resource_copy as cc WHERE rr.id=cc.resource_id AND cc.format_id IN ";
						$query .= " (".$resource_format_CSV.") ";
						//User can CAN VIEW copy
						$query .= " AND (cc.u_id IN (SELECT id1 FROM friend WHERE id2='".$_SESSION['userid']."' AND cc.share_level <= view_setting ) ";
						$query .= " OR cc.u_id='".$_SESSION['userid']."' ) ) ";
					}
					
					//SEARCH STRING
					if(isset($_POST['search-text']) && $_POST['search-text'] != "" ){
						$query .= " AND r.title LIKE '%";
						$query .= $_POST['search-text']."%' ";
					}                   
					
					$query .= " ORDER BY r.title ";
					
					//DEBUG - Print out query incase there is error
                    //print "<br/>".$query."<br/>";
					
					//Run the Query to retrieve requested resources
					$stmt = $mysql->run_query($query);					
					$stmt->bind_result($rid,$title,$year, $type);
					
					$mysql_formats = new Mysql();
					while($stmt->fetch()){
						print "<tr>";
							print "<td>";
								print "<a href='view_resource.php?resource_id=$rid' >".$title."</a>";
							print "</td>";
                            print "<td>";
								print "$year";
							print "</td>";
							print "<td>$type</td>";
							print '<td>';
								$query = "SELECT f.name, c.format_id, count(c.id)
											FROM resource_copy c, resource r, format f
											WHERE c.resource_id=r.id AND f.id=c.format_id AND r.id=? 
											AND c.u_id IN (SELECT id1 FROM friend WHERE id2 = ?  AND c.share_level <= borrow_setting ) 
											GROUP BY c.format_id";
								$stmt_formats = $mysql_formats->run_query($query,$rid,$_SESSION['userid']);
					
								$stmt_formats->bind_result($fname,$fid,$fcount);
								$count_formats = 0;
								while($stmt_formats->fetch()){
									print " (".$fcount.") ".$fname;
									$count_formats++;
								}
								$stmt_formats->close();
								if($count_formats == 0){
									print "-----";
								}
								
							print '</td>';
						print "</tr>";
						
					}
					$stmt->close();		
				?>
			</table>
			</div>
      </div>
      <!-- end div#content -->
      <div id="sidebar">
      <form method="post" action="">
        <ul>
          <li id="search">
            <h1>Search</h1>
            
              <fieldset>
                <input type="text" id="search-text" name="search-text" value="" />
                <input type="submit" id="search-submit" value="Search" />
              </fieldset>
            
          </li>
          <li>
            <h2>Owner</h2>
            <ul>
              <li><input type="checkbox" id="mine" name="mine" value="mine" <?php if(isset($_POST['mine'])) print "CHECKED"; ?>/>Mine</li>
				<!-- 
				<ul>
					<li><input type="checkbox" id="loaned" name="loaned" value="loaned" <?php //if(isset($_POST['loaned'])) print "CHECKED"; ?> />Loaned Out</li>
				</ul>
				-->
              <li><input type="checkbox" id="friends" name="friend" value="friend" <?php if(isset($_POST['friend'])) print "CHECKED"; ?> />Friend's</li>
				<ul>
					<li><input type="checkbox" id="canborrow" name="canborrow" value="canborrow"  <?php if(isset($_POST['canborrow'])) print "CHECKED"; ?>/>Can Borrow</li>
				</ul>
            </ul>
          </li>
          <li>
            <h2>Media Type</h2>
            <ul>
            <?php
            $query_type = "SELECT name, id FROM resource_type";
            $stmt_type = $mysql->run_query($query_type);					
            $stmt_type->bind_result($type_name, $type_id);				
            
            //Print out media types
            while($stmt_type->fetch()){
                $type_name_a[] = $type_name;
                $type_id_a[] = $type_id;
            }            
            $stmt_type->close();            
            $type_count = count($type_name_a);
            for($i=0;$i<$type_count;$i++){                
                print "<li>";                   
                    print "<input type='checkbox' id='".$type_name_a[$i]."' name='type[]' value='".$type_id_a[$i]."'";
                    if (in_array($type_id_a[$i], $resource_type)) print "CHECKED";
                    print "/>".$type_name_a[$i];   
                    print "</li>\n"; 
                    print "<ul>\n";
                    //GET Formats for this type
                    $query_format = "SELECT name, id FROM format WHERE type_id='".$type_id_a[$i]."'";
                    $stmt_format = $mysql->run_query($query_format);					
                    $stmt_format->bind_result($format_name, $format_id);
                    
                    while($stmt_format->fetch()){
                        print "<li>";
                        print "<input type='checkbox' id='$format_name' name='format[]' value='$format_id' ";
                        if (in_array($format_id, $resource_format)) print "CHECKED";
                        print "/>".$format_name;
                        print "</li>\n";
                    }
                    $stmt_format->close();
                    print "</ul>\n";
                               
            }            		
            ?>
          </ul>
        </ul>
        </form>
      </div>
      <!-- end div#sidebar -->
      <div style="clear: both; height: 1px"></div>
    </div>
  </div>
  <!-- end div#page -->
    <?php include('includes/footer.php'); ?>
</div> <!-- end div#wrapper -->
</body>
</html>

